package com.firmboy.thread;

/**
 * 测试java的Wait方法
 *
 * @author firmboy
 * @create 2018-05-03 上午10:37
 **/
public class WaitTest {




    public static void main(String[] args){


        Runnable run = new Runnable() {
            @Override
            public synchronized void run() {
                if(Thread.currentThread().getName().equals("线程3")){
                    System.out.println("我是线程3");
                    this.notifyAll();
                    return;
                }
                for (int i=0;i<5;i++){
                    if(i==1){
                        try{
                            this.wait();
                            System.out.println(Thread.currentThread().getName()+":我还没有被执行到！");
                        }catch(Exception e){
                            e.printStackTrace();
                        }

                    }
                    System.out.println(Thread.currentThread().getName()+":当前的值为--->"+i);
                }

            }
        };


        Thread th1 = new Thread(run);
        th1.setName("线程1");
        Thread th2 = new Thread(run);
        th2.setName("线程2");



        th1.start();
        th2.start();

        Thread th3 = new Thread(run);
        th3.setName("线程3");
        th3.start();


    }

}
